package com.yehuishou.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.Classify;
import com.yehuishou.pojo.Models;
import com.yehuishou.pojo.Oditem;
import com.yehuishou.pojo.QueryVO;
import com.yehuishou.service.SaleOrderService;

@Controller
public class SaleOrderController {
		
	@Autowired
	private SaleOrderService saleOrderService;

	@RequestMapping("/content-3_2")
	public String SaleOrder(@RequestParam(required=true,defaultValue="1") Integer pageNum,
			@RequestParam(required=true,defaultValue="10") Integer pageSize,QueryVO vo,HttpServletRequest request,ModelMap mm) {
		vo.getOrder().setOrder_type(2);
		PageInfo<QueryVO> mv= saleOrderService.findUserOrder(pageNum,pageSize,vo);
		if(vo != null && vo.getOrder() != null){
			SimpleDateFormat onetime =new SimpleDateFormat("yyyy-MM-dd");
			if(vo.getOrder().getStartdate() != null) {
				Date startDate = vo.getOrder().getStartdate();
				String format2 = onetime.format(startDate.getTime());
				request.setAttribute("onetime", format2);
			}
			if(vo.getOrder().getEnddate() != null) {
				Date startDate1 = vo.getOrder().getEnddate();
				String format = onetime.format(startDate1.getTime());
				request.setAttribute("endtime", format);
			}
		}
		//将信息传到页面
		request.setAttribute("mv", mv);
		//数据回显
		request.setAttribute("vo", vo);
		return "content-3_2";
	}
	
	@ResponseBody
	@RequestMapping("/deleteUser")
	public boolean deleteUser(String oid) {
		
		Integer integer1   =	 saleOrderService.deleteUser(oid);
		Integer integer2   =  	 saleOrderService.deleteItemOrder(oid);
		return true;
	}
	
	/**
	 * 通过 订单编号oid 查询 订单oid 下面的所有商品
	 * @param oid
	 * @return
	 */
	@RequestMapping("/details")
	public ModelAndView detailsById(String oid) {
		ModelAndView mv = new ModelAndView("content-3_2.1");
		List<Oditem> od = saleOrderService.finditems(oid);
		mv.addObject("mv",od);
		return mv;
	}
	
	//删除订单明细
	@ResponseBody
	@RequestMapping("/detailsDelete")
	public Boolean UpdateItem(Integer i_id) {
		
		Integer integer = saleOrderService.detailsDelete(i_id);
		System.out.println(integer+"1");
		return true;
	}
	
	//通过ID 查询 单个订单明细 返回到页面用于修改
	@RequestMapping("/FindDetailsById")
	public ModelAndView DetailsById(Oditem oditem) {
		ModelAndView mv =new ModelAndView();
		Oditem od = saleOrderService.FindDetailsById(oditem);
		List<Classify> classify = saleOrderService.selclassify();
		List<Brand> brand = saleOrderService.selbrand();
		List<Models> models =saleOrderService.selmodel();
		mv.addObject("od",od);
		mv.addObject("classify",classify);
		mv.addObject("brand",brand);
		mv.addObject("models",models);
		mv.setViewName("content-3_2.2");
		return mv;
	}
	
	//更新订单明细
	@RequestMapping("/updateOitem")
	public String UpdateDetails(Oditem oditem) {
		Integer od = saleOrderService.UpdateDetails(oditem);
		Integer ct = saleOrderService.UpdateConfiguration(oditem);
		return "redirect:details?oid="+oditem.getOid();
	}
	
		// 批量删除的ajax
		@ResponseBody
		@RequestMapping("/deleteOrder")
		public boolean deleteBackItems(@RequestParam(value = "oids[]") String[] oids) {
	           System.out.println("-------"); 
				saleOrderService.deleteUsers(oids);
				saleOrderService.deleteItemOrders(oids);
			return true;
			
	}  

}
